from flask_restful import Resource

from model.crud import QueryOneInstance, QueryMultipleInstances
from model.tables import User, Like, Blog
from restful.atomic.info.like._Like import _Like
from restful.data import DataUid
from restful.error import RenderUserNotExist
from restful.response import ResponseSuccess
from restful.session import login_required,data

#likes 用户点赞过的所有帖子
#get_likes 用户的所有帖子受到点赞
class UserLike(Resource):
    @data(DataUid())
    def get(self,data):
        user = QueryOneInstance(User,{'id':data['uid']})

        if not user:
            return RenderUserNotExist(data['uid'])

        likes = QueryMultipleInstances(Like,{'uid':data['uid']})
        pids = [blog.id for blog in Blog.query.filter_by(
            uid=data['uid'],
        ).all()]

        get_likes = Like.query.filter(Like.pid.in_(pids)).all()

        return ResponseSuccess(data={
            'likes':_Like(likes),
            'get_likes':_Like(get_likes),
        })